package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.mapper.StrategyCommendMapper;
import cn.wolfcode.luowowo.article.query.StrategyCommendQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class StrategyCommendServiceImpl implements IStrategyCommendService {

    @Autowired
    private StrategyCommendMapper strategyCommendMapper;

    @Autowired
    private StringRedisTemplate template;

    @Override
    public PageInfo query(StrategyCommendQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyCommendMapper.selectForList(qo));
    }

    @Override
    public List<StrategyCommend> getTop5() {
        return strategyCommendMapper.selectTop5();
    }

    @Override
    public List<StrategyStatisVO> getHotVOTop10() {
        String setKey = RedisKeys.STRATEGY_ZEST_SORT_HOT.getPrefix();
        //zset中排序的值,也是vo对象key的集合
        Set<String> values = template.opsForZSet().reverseRange(setKey, 0, 9);
        ArrayList<StrategyStatisVO> list = new ArrayList<>();
        if (values != null && values.size() > 0) {
            for (String vokey : values) {
                String voStr = template.opsForValue().get(vokey);
                list.add(JSON.parseObject(voStr, StrategyStatisVO.class));
            }
        }
        return list;
    }
}
